<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>

  <style type="text/css">
    html, body {
      width: 100%;
      height: 100%;
      margin: 0;
      padding: 0;
      overflow: hidden;
    }

    #map {
      width: 100%;
      height: 100%;
    }
  </style>
</head>
<body>

<div id="map"></div>
<canvas id="canvas"></canvas>

<script type="text/javascript" src="//apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="//api.map.baidu.com/api?v=3.0&ak=baiduKey"></script>
<script type="text/javascript" src="../../js/mapv.js"></script>

<script type="text/javascript">

  // 百度地图API功能
  var map = new BMap.Map("map", {
    enableMapClick: false
  });    // 创建Map实例
  map.centerAndZoom(new BMap.Point(105.403119, 38.028658), 5);  // 初始化地图,设置中心点坐标和地图级别
  map.enableScrollWheelZoom(true); // 开启鼠标滚轮缩放

  map.setMapStyle({
    styleJson: [
      {
        "featureType": "all",
        "elementType": "all",
        "stylers": {
          "lightness": 61,
          "saturation": -100
        }
      }
    ]
  });


  var randomCount = 500;

  var data = [];

  var citys = ["北京","天津","上海","重庆","石家庄","太原","呼和浩特","哈尔滨","长春","沈阳","济南","南京","合肥","杭州","南昌","福州","郑州","武汉","长沙","广州","南宁","西安","银川","兰州","西宁","乌鲁木齐","成都","贵阳","昆明","拉萨","海口"];

  // 构造数据
  while (randomCount--) {
    var cityCenter = mapv.utilCityCenter.getCenterByCityName(citys[parseInt(Math.random() * citys.length)]);
    data.push({
      geometry: {
        type: 'Point',
        coordinates: [cityCenter.lng - 2 + Math.random() * 4, cityCenter.lat - 2 + Math.random() * 4]
      },
      count: parseInt(4 * Math.random())
    });
  }

  var allDataSet = new mapv.DataSet(data);
  var dataSet = new mapv.DataSet(data);

  var options = {
    splitList: {
      other: 'white',
      1: 'blue',
      2: 'yellow',
      3: 'red'
    },
    size: 2,
    max: 30,
    draw: 'category'
  }

  var mapvLayer = new mapv.baiduMapLayer(map, dataSet, options);

  var legend = mapvLayer.getLegend();

  legend.style.position = 'absolute';
  legend.style.left = '10px';
  legend.style.bottom = '50px';

  document.body.appendChild(legend);

  $(legend).delegate('div', 'click', function () {
    var value = $(this).attr('value');
    var data = allDataSet.get({
      filter: function (item) {
        if (item.count == value) {
          return true;
        }
      }
    });
    dataSet.set(data);
  });

</script>

</body>
</html>
